﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System.Reflection
{
	internal class DynamicCacheFactory<T>
    {
        Dictionary<int, T> caches = new Dictionary<int, T>();

        private static volatile DynamicCacheFactory<T> instance;

        private static object syncRoot = new Object();

        public static DynamicCacheFactory<T> Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new DynamicCacheFactory<T>();
                        }
                    }
                }
                return instance;
            }
        }

        public bool Contains(int key)
        {
            return caches.ContainsKey(key);
        }

        public T GetValue(int key)
        {
            if (!caches.ContainsKey(key))
                return default(T);

            return caches[key];
        }

        public void AddValue(int key, T value)
        {
            if (caches.ContainsKey(key))
                return;

            caches.Add(key, value);
        }

        public void ClearAll()
        {
            caches.Clear();

        }
    }
}
